/*****************************************************************
*	Spi.c :
*	-------
*	->	Fichier source regroupant :
*			+ les fonctions d'acquisition SPI sur 8 ou 24 bits
*
*	Par Le Roux Nicolas (MIQ4)
*	Le 01/07/2011	Stage Drone 2011
*****************************************************************/


#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include <p18f2520.h>
#include "init.h"
#include "Spi1.h"
#include "spi.h"

/*
 ********************************************************************************************************
 **                                                                                                    **
 **                                        Acquisition de donnee sur 24 bits                           **
 **                                                                                                    **
 ********************************************************************************************************
*/

unsigned short long EchangeSpiU24(void)
{
	union _DWORD_VAL dwTemp;				//variable 32 bits.
	unsigned char ucTemp;


	dwTemp.byte.UB = EchangeU08();
	dwTemp.byte.HB = EchangeU08();
	dwTemp.byte.LB = EchangeU08();

	return (unsigned short long)( dwTemp.Val & 0x00FFFFFF);

}


/*
/********************************************************************************************************
 **                                                                                                    **
 **                                        Acquisition de donnee sur 8 bits                            **
 **                                                                                                    **
 ********************************************************************************************************
*/

unsigned char EchangeU08(void)
{
	unsigned char ucTemp;

	ucTemp = SSPBUF;        		// Clear BF
	PIR1bits.SSPIF = 0;      		// Clear interrupt flag
	SSPBUF = 0x00;                  // initiate bus cycle
//	while ( !SSPSTATbits.BF );      // wait until cycle complete
	while(!PIR1bits.SSPIF);  		// wait until cycle complete
	return ( SSPBUF );              // return with byte read

}

